import React from 'react';
import Icon, { createFromIconfontCN } from '@ant-design/icons';
import defaultSettings from '../../../config/defaultSettings';
import * as allIcons from '@ant-design/icons';

const IconFont = createFromIconfontCN({
  scriptUrl: defaultSettings.iconfontUrl,
});

type SvgIconProps = {
  icon?: string | React.ReactNode;
  style?: object;
};

const SvgIcon: React.FC<SvgIconProps> = ({ icon, style }) => {
  if (typeof icon === 'string') {
    if (icon.startsWith('http')) {
      return <Icon component={() => <img src={icon} alt="icon" style={style} />} />;
    }
    if (icon.startsWith('icon-')) {
      return <IconFont type={icon} />;
    }
    return <Icon component={allIcons[`${icon}Outlined`]} style={style} />; //React.createElement(`${icon}Outlined`)
  }
  return <>{icon}</>;
};

export default SvgIcon;
